<template>
  <div id="add-blog" class="container">
    <h1>添加博客</h1>
    <form v-if="!summbited" >
      <div class="form-group">
          <label for="title">博客标题:</label>
          <input type="text" class="form-control" id="title" autocomplete="off" value="title" v-model="blog.title">
      </div>
      <div class="form-group">
          <label for="content">添加内容:</label>
          <textarea v-model="blog.content" class="form-control" rows="5" id="content" value="content"></textarea>
      </div>
      
      <div class="chechbox">
        <input type="checkbox" value="vue.js" v-model="blog.categories">
        <label for="">vue.js</label>

        <input type="checkbox" value="react.js" v-model="blog.categories">
        <label for="">react.js</label>

        <input type="checkbox" value="node.js" v-model="blog.categories">
        <label for="">node.js</label>

        <input type="checkbox" value="Angular4" v-model="blog.categories">
        <label for="">Angular4</label>
      </div>
      <div class="form-group">
          <label>作者:</label>
          <select v-model="blog.author" class="form-control">
            <option v-for="author in authors" :key="author.id">
              {{author}}
            </option>
          </select>
      </div>
      
      
      <button type="submit" class="btn btn-primary" @click.prevent="Post">博客保存</button>
    </form>
    <div v-if="summbited">
      <h3>你的博客已经发布成功了！</h3>
    </div>

    <hr>
    <div class="preview">
      <h3>博客总览</h3>
      <p>博客标题:</p>
      <p>{{blog.title}}</p>
      <p>博客内容:</p>
      <p>{{blog.content}}</p>
      <p>博客分类:</p>
      <ul>
        <li v-for="category in blog.categories" :key="category.id">
          {{category}}
        </li>
      </ul>
      <p>作者:</p>
      <p>{{blog.author}}</p>
    </div>

  </div>
</template>

<script>
export default {
  name: "add-blog",
  data() {
    return {
      blog: {
        title: "",
        content: "",
        author: "",
        categories: []
      },
      authors: ["lifeng", "zhangsan", "lisi"],
      summbited: false
    };
  },
  methods: {
    Post: function() {
      this.$http
        .post("https://vuedemo-123.firebaseio.com//posts.json", this.blog)
        .then(data => {
          console.log(data);
          this.summbited = true;
        });
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.preview {
  margin: 30px 0px;
  border: 1px dotted #ccc;
  padding: 10px 20px;
}
</style>
